package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;

import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;

import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repostiory.StrategyRepository;

import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;

import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;


@Service
//@Transactional  暂时先别管事务
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repository;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private  IStrategyService strategyService;
    @Autowired
    private MongoTemplate template;


    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //按主题id查询
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");

        return DBHelper.query(template, Strategy.class, query,pageable);
    }

    //注意 处理额外的数据维护
    @Override
    public void saveOrUpdate(Strategy strategy) {
        //目的地id name
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());
        //主题名称
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //分类 名称
        strategy.setCatalogName(strategyCatalog.getName());
        //是否国外
        List<Destination> toasts = destinationService.queryToasts(strategyCatalog.getDestId());
        //取出第一个
        if (toasts!=null&&toasts.size()>0){

            Destination destination = toasts.get(0);
            if ("中国".equals(destination.getName())){

                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }


        if(StringUtils.hasLength(strategy.getId())){
            //查询
            Strategy stra = strategyService.get(strategy.getId());
            //替换
            strategy.setThemeName(stra.getThemeName());
            strategy.setFavornum(stra.getFavornum());
            strategy.setCreateTime(stra.getCreateTime());
            strategy.setViewnum(stra.getViewnum());
            strategy.setSharenum(stra.getSharenum());
            strategy.setReplynum(stra.getReplynum());
            //更新

            repository.save(strategy);
        }else{
            //添加
            strategy.setId(null);
            //创建时间

            strategy.setCreateTime(new Date());
            repository.save(strategy);

        }

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }
    //局部修改
    @Override
    public void changeState(String id, int state) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByCatalogId(String id) {
        return repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {

        //条件因为我们的表中有一个冗余字段destId可以直接用它来查出对应的攻略
        Pageable  pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy> list =  repository.findByDestId(destId,pageable);

        return list;
    }
    //持久化
    @Override
    public void updateStrategyStatisVO(StrategyStatisVO strategyStatisVO) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(strategyStatisVO.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",strategyStatisVO.getViewnum());
        update.set("replynum",strategyStatisVO.getReplynum());
        update.set("favornum",strategyStatisVO.getFavornum());
        update.set("sharenum",strategyStatisVO.getSharenum());
        update.set("thumbsupnum",strategyStatisVO.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestName(String keyword) {

        return repository.queryByDestName(keyword);
    }


}
